package com.inossem.warehousestorage_inossem.activity.receive;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.receive.extra.ReceiveExtra;
import com.inossem.warehousestorage_inossem.adapter.receive.ReceiveItemListAdapter;
import com.inossem.warehousestorage_inossem.adapter.receive.ReceiverListAdapter;
import com.inossem.warehousestorage_inossem.bean.receive.ReceiveReceiveItemBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.receive.ReceiveReceiveDetailRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.ReceiveUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;
import com.zhy.android.percent.support.PercentFrameLayout;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.ArrayList;
import java.util.List;

public class ReceiveReceiveDetailActivity extends BaseActivity {

    private static final int OVERVIEW = 0;

    private static final int DETAIL = 1;

    private static final int RECEIVER = 2;

    private TextView title, receiveNumber, purchase, supplier, overview, detail, receiver,submitLine;

    private PullToRefreshLayout goodsRefresh, receiverRefresh;

    private PullableListView goodsList, receiverList;

    private String deliveryNumberStr, receiveNumberStr, mGoodsNumber = "";

    private List<ReceiveReceiveItemBean.DataBean.GoodsListBean> allData;

    private ReceiveItemListAdapter receiveItemListAdapter;

    private ReceiverListAdapter receiverListAdapter;

    private PercentFrameLayout overviewLayout, detailLayout, receiverLayout;

    private PercentRelativeLayout submitOverviewLayout;

    private List<PercentFrameLayout> layoutList = new ArrayList<>();


    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_receive_receive_detail);
        initView(ReceiveReceiveDetailActivity.this);
        SPUtils.removeReceiveInformation(ReceiveReceiveDetailActivity.this);
    }

    @Override
    protected void onResumeImpl() {
        initData(ReceiveReceiveDetailActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        goodsRefresh = (PullToRefreshLayout) activity.findViewById(R.id.goodsListRefresh);
        goodsList = (PullableListView) activity.findViewById(R.id.goodsList);
        receiverRefresh = (PullToRefreshLayout) activity.findViewById(R.id.receiverListRefresh);
        receiverList = (PullableListView) activity.findViewById(R.id.receiverList);
        receiveNumber = (TextView) activity.findViewById(R.id.receiveNumber);
        purchase = (TextView) activity.findViewById(R.id.purchase);
        supplier = (TextView) activity.findViewById(R.id.supplier);
        submitLine = (TextView) activity.findViewById(R.id.submitLine);
        submitOverviewLayout = (PercentRelativeLayout) activity.findViewById(R.id.submitOverviewLayout);
        overviewLayout = (PercentFrameLayout) activity.findViewById(R.id.overviewLayout);
        detailLayout = (PercentFrameLayout) activity.findViewById(R.id.detailLayout);
        receiverLayout = (PercentFrameLayout) activity.findViewById(R.id.receiverLayout);
        overview = (TextView) activity.findViewById(R.id.overview);
        detail = (TextView) activity.findViewById(R.id.detail);
        receiver = (TextView) activity.findViewById(R.id.receiver);
    }

    private void showItem(int index) {
        for (int i = 0; i < layoutList.size(); i++) {
            layoutList.get(i).setVisibility(View.GONE);
        }
        layoutList.get(index).setVisibility(View.VISIBLE);
//        for (int i = 0; i < layoutList.size(); i++) {
//            LogUtils.i(i + "---Visibility:" + layoutList.get(i).getVisibility());
//        }
    }

    private void initData(Activity activity) {
        title.setText(activity.getResources().getString(R.string.activity_receive_receive_detail_title));
        ActivityUtils.initBackAndFunction(activity, R.string.activity_receive_delivery_overview_title_short, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                ReceiveReceiveDetailActivity.this.finish();
            }
        });
        layoutList.add(overviewLayout);
        layoutList.add(detailLayout);
        layoutList.add(receiverLayout);

        goodsList.setPullUpFlag(false);
        goodsList.setPullDownFlag(false);
        receiverList.setPullUpFlag(false);
        receiverList.setPullDownFlag(false);
        getExtra(activity);
        mGoodsNumber = "";
        getData(activity, deliveryNumberStr, receiveNumberStr, mGoodsNumber);
        overview.setOnClickListener((View v) -> {
            showItem(OVERVIEW);
        });
        detail.setOnClickListener((View v) -> {
            showItem(DETAIL);
        });
        receiver.setOnClickListener((View v) -> {
            showItem(RECEIVER);
        });
    }

    private void getExtra(Activity activity) {
        deliveryNumberStr = activity.getIntent().getStringExtra(ReceiveExtra.DELIVERY_NUMBER);
        receiveNumberStr = activity.getIntent().getStringExtra(ReceiveExtra.RECEIVE_NUMBER);
    }

    private void getData(Context context, String deliveryNumberStr, String receiveNumberStr, String goodsNumber) {
        String receiveInformation = SPUtils.getReceiveInformation(context);
        if (TextUtils.isEmpty(receiveInformation)) {
            ReceiveReceiveDetailRequest.request(context, deliveryNumberStr, receiveNumberStr, new GsonBeanListener<ReceiveReceiveItemBean>() {
                @Override
                public void onSuccess(ReceiveReceiveItemBean bean) {
                    Gson gson = new Gson();
                    String json = gson.toJson(bean, ReceiveReceiveItemBean.class);
                    SPUtils.saveReceiveInformation(context, json);
                    allData = bean.getData().getGoodsList();
                    fileData(context, bean);
                }
            });
        } else {
            Gson gson = new Gson();
            ReceiveReceiveItemBean bean = gson.fromJson(receiveInformation, ReceiveReceiveItemBean.class);
            if (TextUtils.isEmpty(goodsNumber)) {
                allData = bean.getData().getGoodsList();
            } else {
                allData = ReceiveUtils.getGoodsList(context, goodsNumber);
            }
            fileData(context, bean);
        }
    }

    private void addGoodsListData(Context context, List<ReceiveReceiveItemBean.DataBean.GoodsListBean> goodsList, ListView listView, boolean flag) {
        if (flag) {
            receiveItemListAdapter = new ReceiveItemListAdapter(context, goodsList, mGoodsNumber, true, (String goodsNumber) -> {
                mGoodsNumber = goodsNumber;
                getData(context, deliveryNumberStr, receiveNumberStr, mGoodsNumber);
            });
            listView.setAdapter(receiveItemListAdapter);
        }
    }

    private void addReceiverListData(Context context, ReceiveReceiveItemBean.DataBean receiverList, ListView listView) {
        receiverListAdapter = new ReceiverListAdapter(context, receiverList, true, () -> {
        });
        listView.setAdapter(receiverListAdapter);
    }

    private void fileData(Context context, ReceiveReceiveItemBean bean) {
        addGoodsListData(context, ActivityUtils.getShowData(allData, 0), goodsList, true);
        addReceiverListData(context, bean.getData(), receiverList);
        receiveNumber.setText(bean.getData().getReceiveNumber());
        purchase.setText(bean.getData().getPurchaseNumber() + " - " + bean.getData().getPurchaseType());
        supplier.setText(bean.getData().getSupplier());
        submitLine.setText(bean.getData().getGoodsList().size()+"");
        submitOverviewLayout.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                DialogUtils.getOverviewDialog(context,ReceiveUtils.getOverViewBeanWithGoodsList(bean.getData().getGoodsList())).show();
            }
        });
        showItem(OVERVIEW);
    }

}
